package cn.iocoder.yudao.module.cmt.service.activity;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.module.cmt.controller.admin.activity.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.activity.ActivityCategoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cmt.dal.mysql.activity.ActivityCategoryMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cmt.enums.ErrorCodeConstants.*;

@Service
@Validated
public class ActivityCategoryServiceImpl implements ActivityCategoryService {

    @Resource
    private ActivityCategoryMapper activityCategoryMapper;

    @Override
    public Long createActivityCategory(ActivityCategorySaveReqVO createReqVO) {
        ActivityCategoryDO activityCategory = BeanUtils.toBean(createReqVO, ActivityCategoryDO.class);
        activityCategoryMapper.insert(activityCategory);
        return activityCategory.getId();
    }

    @Override
    public void updateActivityCategory(ActivityCategorySaveReqVO updateReqVO) {
        validateActivityCategoryExists(updateReqVO.getId());
        ActivityCategoryDO updateObj = BeanUtils.toBean(updateReqVO, ActivityCategoryDO.class);
        activityCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityCategory(Long id) {
        validateActivityCategoryExists(id);
        activityCategoryMapper.deleteById(id);
    }

    private void validateActivityCategoryExists(Long id) {
        if (activityCategoryMapper.selectById(id) == null) {
            throw exception(ACTIVITY_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public ActivityCategoryDO getActivityCategory(Long id) {
        return activityCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<ActivityCategoryDO> getActivityCategoryPage(ActivityCategoryPageReqVO pageReqVO) {
        return activityCategoryMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteActivityCategoryListByIds(List<Long> ids) {
        ids.forEach(this::validateActivityCategoryExists);
        activityCategoryMapper.deleteBatchIds(ids);
    }

}
